# -*- coding: utf-8 -*-
import scrapy
import json
from scrapy.loader import ItemLoader
from scrapy import Request,FormRequest
from scrapy.loader.processors import Join,MapCompose
import re
from bookcrawl.items import BookItem,LinkItem

'''
暂时停止该网站的爬虫解析
'''

# product_url = "https://www.amazon.cn/gp/product/{pid}"
re_isbn = re.compile("[iI][sS][bB][nN][:：]([0-9\-]+)")
re_press = re.compile("出版社[:：](.+)")
re_pid = re.compile("product-([0-9]+)")

class BeixinSpider(scrapy.Spider):
    # name = 'beixin'
    # allowed_domains = ['amazon.cn']
    # start_urls = ['https://www.amazon.cn/gp/product/B07KBT5149']
    custom_settings = {
        "DOWNLOAD_DELAY": 1,
        "CONCURRENT_REQUESTS_PER_DOMAIN": 1
    }

    def parse(self, response):
        loader = ItemLoader(item=BookItem(),response=response)
        loader.default_input_processor = MapCompose(str)
        loader.default_output_processor = Join(" ")

        url = response.url
        pid = re.search(re_pid,url).group(1)

        loader.add_xpath("title", '//title/text()')

        loader.add_value("pid",pid)
        loader.add_value("link",url)


        frm = {
            "id":pid,
        }

        item = loader.load_item()
        yield item

        rloader = ItemLoader(item=LinkItem())
        if hasattr(response.meta,"relation"):
            rloader.add_value("relation",response.meta["relation"])
            rloader.add_value("pid",response.meta["pid"])
            rloader.add_value("link_pid",pid)
            yield rloader.load_item()


    def parse_info(self,response):
        loader = response.meta["loader"]

        base = response.xpath('//*[@class="book-info"]/p/text()')

        for info in base:
            text = info.get()
            if re.search(re_isbn, text) is not None:
                isbn = re.search(re_isbn, text).group(1)
                loader.add_value("isbn",isbn)
            elif re.search(re_press,text) is not None:
                press = re.search(re_press,text).group(1)
                loader.add_value("press",press)

        loader.add_value("price",'//*[@id="priceListStr"]/@value')
        loader.add_value("author",'//*[@class="writer"]/span/text()')

